package com.kjlink.internal.workreport.user.model;


import cn.xuqiudong.common.base.model.BaseEntity;
import cn.xuqiudong.quartz.helper.JobUserHolder;
import com.kjlink.internal.workreport.base.annotation.NoneColumn;
import com.kjlink.internal.workreport.base.constant.CommonConstant;
import com.kjlink.internal.workreport.base.enums.UserType;
import com.kjlink.internal.workreport.helper.SessionHelper;
import com.kjlink.internal.workreport.third.OaUser;
import com.kjlink.internal.workreport.util.NumberUtils;


/**
 * 用户,来自oa 实体类
 *
 * @author Vic.xu
 */
public class User extends BaseEntity {

    private static final long serialVersionUID = 1L;

    /**
     * 执行定时任务用户
     */
    public static final User JOB_USER;

    static {
        JOB_USER = new User();
        JOB_USER.setId(-2);
        JOB_USER.setUserCode("-002");
        JOB_USER.setUserName("quartz_user");
        JOB_USER.setType(UserType.admin);
    }
    /**
     * userCode
     */
    private String userCode;

    /**
     * 用户名
     */
    private String userName;

    /**
     * 密码Upper md5
     */
    private String password;

    /**
     * 类型：mormal/team/admin
     */
    private UserType type;

    /**
     * 是否开发经理
     */
    private Boolean devManager;

    /**
     * 所属小组
     */
    private Integer groupId;

    /**
     * 小组名称
     */
    @NoneColumn
    private String groupName;

    /**
     * jira账号 - 经办人
     */
    private String assignee;

    public User() {
    }

    public User(OaUser oaUser) {
        this.setUserName(oaUser.getUsername());
        this.setPassword(oaUser.getPassword());
        this.setUserCode(oaUser.getUsercode());
    }

    /***************** set|get  start **************************************/
    /**
     * set：userCode
     */
    public User setUserCode(String userCode) {
        this.userCode = userCode;
        return this;
    }

    /**
     * get：userCode
     */
    public String getUserCode() {
        return userCode;
    }

    /**
     * set：用户名
     */
    public User setUserName(String userName) {
        this.userName = userName;
        return this;
    }

    /**
     * get：用户名
     */
    public String getUserName() {
        return userName;
    }

    /**
     * set：密码Upper md5
     */
    public User setPassword(String password) {
        this.password = password;
        return this;
    }

    /**
     * get：密码Upper md5
     */
    public String getPassword() {
        return password;
    }

    /**
     * set：类型：mormal/team/admin
     */
    public User setType(UserType type) {
        this.type = type;
        return this;
    }

    /**
     * get：类型：mormal/team/admin
     */
    public UserType getType() {
        return type;
    }

    /**
     * set：所属小组
     */
    public User setGroupId(Integer groupId) {
        this.groupId = groupId;
        return this;
    }

    /**
     * get：所属小组
     */
    public Integer getGroupId() {
        return groupId;
    }

    public Boolean getDevManager() {
        return devManager;
    }

    public void setDevManager(Boolean devManager) {
        this.devManager = devManager;
    }


    /***************** set|get  end **************************************/

    public String getGroupName() {
        return groupName;
    }

    public void setGroupName(String groupName) {
        this.groupName = groupName;
    }


    public String getAssignee() {
        return assignee;
    }

    public void setAssignee(String assignee) {
        this.assignee = assignee;
    }

    /**
     * 是否可编辑用户
     *
     * @return boolean
     */
    public boolean getCouldEdit() {
        User user = SessionHelper.currentUser();
        if (user == null) {
            return false;
        }
        if (UserType.admin == user.getType()) {
            return true;
        }
        if (NumberUtils.equals(this.id, user.getId())) {
            return true;
        }
        return CommonConstant.SPECIAL_USER_CODE.equals(user.getUserCode());
    }

    public boolean isAdmin() {
        return UserType.admin == this.getType() || CommonConstant.SPECIAL_USER_CODE.equals(getUserCode());
    }

    public boolean isNormal() {
        return UserType.normal == this.getType();
    }

    public boolean isManager() {
        return Boolean.TRUE.equals(this.devManager);
    }

    public String getTypeText() {
        if (this.type == null) {
            return UserType.normal.getText();
        }
        return this.type.getText();
    }

}
